package com.abe.bathsystem.utils;

import android.support.annotation.StringDef;

import com.abe.bathsystem.entity.serial.DeviceControlEntity;
import com.abe.bathsystem.entity.serial.DeviceTemperatureEntity;
import com.abe.bathsystem.entity.serial.DeviceWaterUsageEntity;
import com.abe.bathsystem.entity.serial.SerialDataEntity;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Arrays;

public class SerialPortParse {
    private static final int LimitWaterUsage = SerialPortUtils.getWaterUsage(new byte[]{0x10, 0x00});

    public static final String PARSE_ERROR = "PARSE_ERROR";
    public static final String PARSE_TEMPERATURE = "PARSE_TEMPERATURE";
    public static final String PARSE_WATER_USAGE = "PARSE_WATER_USAGE";
    public static final String PARSE_CLEAR_WATER = "PARSE_CLEAR_WATER";
    public static final String PARSE_CONTROL_VALVE = "PARSE_CONTROL_VALVE";
    public static final String PARSE_WATER_VALVE = "PARSE_WATER_VALVE";
    public static final String PARSE_CONTROL_LOCKER = "PARSE_CONTROL_LOCKER";
    public static final String PARSE_LOCKER = "PARSE_LOCKER";

    @StringDef({PARSE_ERROR, PARSE_TEMPERATURE,
            PARSE_WATER_USAGE, PARSE_WATER_VALVE, PARSE_CLEAR_WATER, PARSE_CONTROL_VALVE,
            PARSE_CONTROL_LOCKER, PARSE_LOCKER})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SerialPortParseDataType {

    }

    public static int parseAddressPos(SerialDataEntity entity) {
        //* 返回原始数据 */
        return entity.getCommand()[3];
    }

    public static @SerialPortParseDataType
    String parse(SerialDataEntity entity) {
        //* 返回原始数据 */
        byte[] command = entity.getCommand();
        byte[] data = entity.getBuffer();
        if (data.length >= 8) {
            byte[] available = Arrays.copyOf(data, 8);
            boolean verifyStatus = CRCChecker.checkBuf(available);//CRC校验
            if (verifyStatus) {
                byte userType = available[1];
                switch (userType) {
                    case SerialPortUtils.type_read:
                        byte address = command[3];
                        if (address == 0x00) {
                            return PARSE_TEMPERATURE;
                        } else {
                            return PARSE_WATER_USAGE;
                        }
                    case SerialPortUtils.type_write:
                        return PARSE_CLEAR_WATER;
                    case SerialPortUtils.type_read_control: {
                        byte controlType = available[2];
                        if (controlType == 0x00) {
                            return PARSE_WATER_VALVE;
                        } else {
                            return PARSE_LOCKER;
                        }
                    }
                    case SerialPortUtils.type_control: {
                        byte controlType = available[2];
                        if (controlType == 0x00) {
                            return PARSE_CONTROL_VALVE;
                        } else {
                            return PARSE_CONTROL_LOCKER;
                        }
                    }
                }
            }
        }
        return PARSE_ERROR;
    }

    //解析温度
    public static DeviceTemperatureEntity parseTemperature(SerialDataEntity entity) {
        //* 返回原始数据 */
        byte[] data = entity.getBuffer();
        byte[] available = Arrays.copyOf(data, 8);
        float temperature = SerialPortUtils.getTemperature(new byte[]{available[3], available[4]});
        return new DeviceTemperatureEntity(temperature);
    }

    //解析读用水量
    public static DeviceWaterUsageEntity parseWaterUsage(SerialDataEntity entity) {
        //* 返回原始数据 */
        byte[] data = entity.getBuffer();
        byte[] available = Arrays.copyOf(data, 8);
        int waterUsage = SerialPortUtils.getWaterUsage(new byte[]{available[3], available[4]});
        return new DeviceWaterUsageEntity(waterUsage, waterUsage >= LimitWaterUsage);
    }

    //解析水阀状态或者储物柜锁状态
    public static DeviceControlEntity parseWaterValve(SerialDataEntity entity) {
        //* 返回原始数据 */
        byte[] data = entity.getBuffer();
        byte[] available = Arrays.copyOf(data, 8);
        return new DeviceControlEntity(available[4] == (byte) 0x00);
    }
}
